﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO.Ports;

namespace Flashloader_Core
{
    internal class Program
    { 
        static int Main(string[] args)
        {
            Parameter Pa = new Parameter();
            if (Pa.ParamListCheck(args) == 0x00)
            {
                if (Message.OpenSerialPort(Pa.SerialPortNum))
                {
                    Console.WriteLine("串口" + Pa.SerialPortNum + "打开成功...");
                    Console.WriteLine("1.检测目标板状态...");
                    if (Message.CheckBootStates() == Message.BootStatus.ON_APP_STATUS)
                    {
                        Console.WriteLine("\t---App正在运行中...");
                    }
                    else if (Message.CheckBootStates() == Message.BootStatus.ON_BOOT_STATUS)
                    {
                        Console.WriteLine("\t---目标板已在Boot中...");
                        Console.WriteLine("2.获取目标板伪随机码...");
                        byte[] Code = new byte[4];
                        Code = Message.CheckRandomCode();
                        if (Code[0] != 0x00 && Code[1] != 0x00 && Code[2] != 0x00 && Code[3] != 0x00)
                        {
                            Console.WriteLine("\t---获取伪随机码：【3】：" + Code[3] + "\t【2】：" + Code[2] + "\t【1】：" + Code[1] + "\t【0】：" + Code[0]);
                            Console.WriteLine("3.开始擦除Flash...");
                            if (Message.EraseFlash(new byte[] { 0x19, 0x29, 0x39, 0x49 }))
                            {
                                Console.WriteLine("\t---Flash擦除成功");
                            }
                            else
                            {
                                Console.WriteLine("\t---Flash擦除失败");
                            }
                        }
                        else
                        {
                            Console.WriteLine("\t---伪随机码获取失败");
                        }
                    }
                }

            }
            Console.ReadLine();
            return 0;
        }
    }
}
